<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@include file="./common/header.jsp" %>
<body>
<%@ include file="./common/top.jsp"%>

<div class="container">
	<form class="form-inline" id="search_form" role="form" style="margin-bottom:20px;">
	  <div class="form-group">
	    <label class="sr-only" for="exampleInputEmail2"></label>
	    <select id="category_search_id" class="form-control" name="categoryId">
				<option value="">所有</option>
		</select>
	  </div>
	  
	  <div class="form-group">
	    <label class="sr-only" for=""></label>
	    <input type="text" class="form-control" name="title" id="title_search_id" placeholder="商品标题"/>
	  </div>
	  
	  <div class="form-group">
		    <label class="sr-only" for=""></label>
  		      <select name="grade" class="form-control" placeholder="产品状态">
  		      	<option value="">所有</option>
  		      	<option value="1">名鞋折扣专场</option>
  		      	<option value="5">今日团购</option>
  		      </select>
	  </div>
	  
	  <button type="button"  id="search_id" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span>搜索</button>
	</form>		
			
				
	<a href="${ctx }/admin/addGoods.jsp" target="main_frame" class="btn btn-primary"><span class="glyphicon glyphicon-plus"></span>新增</a>
	
	<div style="margin-top:20px;">
		<table id="goods_table">
	
		</table>
	
	</div>
	</div>
	
	<div class="popup-modal" id="static_popup"> 
  			<form class="form-inline"  id="static_form">
  				<div style="margin-bottom:20px;">
  					<a href="" id="pop_goods_a" target="_blank"></a>	
  				</div>
  				
	  			<div class="form-group">
		    		<label class="sr-only" for=""></label>
					<input type="text" id="startDate_search_id" name="startDate" class="form-control" style="width:120px;" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" placeholder="统计起始时间">&nbsp;-&nbsp;
	  		 	</div>
	  		 	
	  		 	<div class="form-group">
		    		<label class="sr-only" for=""></label>
					<input type="text" class="form-control" name="endDate" id="endDate_search_id" style="width:120px;" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" placeholder="统计截止时间">
	  		 	</div>
	  			<input type="hidden" name="goodsId" id="popup_goods_id"/>
	  			<button type="button" id="static_search_id" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span>查询</button>
	  			
	  			<div style="margin-top:30px;margin-left:5px;"> 
	  				签收订单：<span id="sign_order_id" style="margin-right:10px;"></span>
	  				拒签订单：<span id="nosign_order_id" style="margin-right:10px;"></span>
	  				签收率：<span id="sing_pcent"></span>
	  			</div> 
	  			 
	  		</form>
  		</div>
	<script>
		$(function(){
			
			//查询签收率
			$("#goods_table").on("click",".sign",function(){
				$("#pop_goods_a").attr("href","${ctx}/detailGoods/"+$(this).data("id")).html($(this).data("title"));
				$("#popup_goods_id").val($(this).data("id"));
				$("#sign_order_id").html("");
				$("#nosign_order_id").html("");
				$("#sing_pcent").html(""); 
				jWei.popUp({
					title:'签收统计',
					popUpId:'static_popup',
					width:'400px'
					 
				});
			});
			
			$("#static_search_id").click(function(){
				var formData = $('#static_form').serialize();
				jQuery.ajax( {
					type : 'POST',
					url : '../admin/goods/staticSign.do',
					dataType : 'json',
					data : formData,
					success : function(obj) {
						if(obj.result){
							$("#sign_order_id").html(obj.obj.staticSign);
							$("#nosign_order_id").html(obj.obj.staticNoSign);
							if(obj.obj.staticSign && obj.obj.staticNoSign){
								var pcent = obj.obj.staticSign/(obj.obj.staticSign+obj.obj.staticNoSign)*100;
								$("#sing_pcent").html(pcent.toFixed(2)+"%"); 
							}
						}else{
							jWei.Tips.error(obj.errorMsg);
						}
					},
					error : function() {
						window.top.jWei.Tips.error("数据库异常！");
					}
				});
			});
			
			if("${user.status}" == 1){
				$(".nav-li").each(function(){
					$(this).show();
				});
				
			}
			$(".nav-li").each(function(){
				
				$(this).click(function(){
					$(".active").removeClass("active");
					$(this).addClass("active");
				});
			});
			//退出系统
			$('#a_logout').click(
				function(){
					if(!confirm("确定要退出系统!")){
						return;
					}
					jQuery.ajax({
						type : 'Get',
						contentType : 'application/json',
						url : '../admin/login/logout.do',
						dataType : 'json',
						success : function(obj) {
							if (obj.result)
								location.href = "./goback.html";
						},
						error : function(data) {
							
						}
					});
				}		
			);
		});
	</script>
	
	<script>
		$(function() {
			var grid = new jWei.Datagrid({
				renderTo : $("#goods_table"),
				tabWidth:"",
				fieldset:false,
				legendTitle:"商品列表",
				url : "../admin/goods/pageList.do",
				colModels : [ 
				             
				{
					name : "goods_keyword",
					displayName : "媒体"
				},
				
				{
					name : "updateDate",
					displayName : "最后更新时间",
				}, 
				{
					name : "goods_title",
					displayName : "标题",
					handler:function(v,data,td,tr){
						td.html('<a target="_blank" href="../detailGoods/'+data.goods.id+'">'+v+'</a>');
					}
				},
				
				{
					name : "goods_picName",
					displayName : "封面图",
					handler:function(v,data,td,tr){
						if(v.startsWith("http")){
							var html = '<img id="pic_id" src="'+v+'" style="width: 60px;height: 60px;" />';
							td.html(html);
						}else{
							var html = '<img id="pic_id" src="../admin/upload/downloadCompress.do?fileName='+v+'" style="width: 60px;height: 60px;" />';
							td.html(html);
						}
						
					}
				},
				
				{
					name:"goods_id",
					displayName : "操作",
					handler:function(v,data,td,tr){
						var mark = data.goods.mark;
						var str ="下架";
						if(mark == -1){
							str = '<span style="color:red;">上架</span>';
						}
						 
						var html = '<input type="text" id="order_id" style="width:50px;" value="'+data.goods.orderKey+'" style="margin-right:8px;"/>'+
							'<a id="top_id" href="#" class="sort" data-id="'+v+'" title="保存排序" style="margin-right:8px;">保存排序</a>'+
							'<a id="" href="./commentList.jsp?id='+v+'" data-id="'+v+'" title="评论" style="margin-right:8px;" target="_blank">评论('+data.commentCount+')</a>'+
							'<a id="edit_id" href="./editGoods.jsp?id='+data.goods.id+'" data-id="'+v+'" title="编辑" target="_blank" style="margin-right:8px;">编辑</a>'+
							'<a id="copy_id" href="#" class="copy"  data-id="'+v+'" title="复制" target="_blank" style="margin-right:8px;">复制</a>'+ 
							'<a id="del_id" href="#" class="del" data-id="'+data.goods.id+'" title="删除" style="margin-right:8px;">删除</a>'+
							'<a id="" href="#" style="margin-right:8px;" class="off" data-id="'+data.goods.id+'">'+str+'</a>'+
							'<a id="sign_id" href="#" class="sign" data-title="'+data.goods.title+'" data-id="'+data.goods.id+'" title="签收统计">签收统计</a>';
						td.html(html);
					}
				}
				
				]
			});
			
			$("#search_id").click(function(){
				grid.search("search_form");
				return false;
			});
		 
			$('#title_search_id').bind('keypress',function(event){
	            if(event.keyCode == "13")    
	            {
	            	$("#search_id").trigger("click");
	            	return false;
	            }
	        });
			 
			$("#search_id").click(function(){
				grid.search("search_form_id");
			});
			jQuery.ajax({
				type : 'Get',
				contentType : 'application/json',
				url : '../admin/category/getAll.do',
				data : {
				},
				dataType : 'json',
				success : function(data) {
					if (data.result) {
						var categoryList = data.obj;
						if(categoryList){
							for(var i = 0 ;i<categoryList.length;i++){
								var category = categoryList[i];
								if(category)
									$("#category_search_id").append("<option value="+category.id+">"+category.name+"</option>");
							}
						}
						
					} else {
						alert('数据库异常,查询数据失败');
					}
				},
				error : function(data) {
					
				}
			});
			
			
		});

		$("#goods_table").on("click",".sort",function(){
			
			var id = $(this).data("id");
			var order = $(this).parent().find("#order_id").val();
			jQuery.ajax({
				type : 'Get',
				contentType : 'application/json',
				url : '../admin/goods/sort.do',
				data : {
					id : id,
					order:order
				},
				dataType : 'json',
				success : function(data) {
					if (data.result) {
						window.top.jWei.Tips.success("排序成功！");
						grid.reload();
					} else {
						window.top.jWei.Tips.success(data.errorMsg);
					}
				},
				error : function(data) {
					
				}
			});
			return false;
		});
		
		
		$("#goods_table").on("click",".del",function(){
				if (!confirm("确定要彻底删除该商品！")) {
					return;
				}
				var delObj = $(this);
				jQuery.ajax({
					type : 'Get',
					contentType : 'application/json',
					url : '../admin/goods/del.do',
					data : {
						id : delObj.data("id")
					},
					dataType : 'json',
					success : function(data) {
						if (data.result) {
							window.top.jWei.Tips.success("删除数据成功");
							window.location.reload(true);
						} else {
							window.top.jWei.Tips.success(data.errorMsg);
						}
					},
					error : function(data) {
						
					}
				});
				
				
		});
		
		
		$("#goods_table").on("click",".copy",function(){
			if (!confirm("确定要复制该商品！")) {
				return false;
			}
			var id = $(this).data("id");
			jQuery.ajax({
				type : 'Get',
				contentType : 'application/json',
				url : '../admin/goods/copy.do',
				data : {
					id : id
				},
				dataType : 'json',
				success : function(data) {
					if (data.result) {
						window.top.jWei.Tips.success("操作成功！");
						window.location.reload(true);
					} else {
						window.top.jWei.Tips.success(data.errorMsg);
					}
				},
				error : function(data) {
					
				}
				
			});
			return false;
		});
		$("#goods_table").on("click",".off",function(){
			if (!confirm("确定要下架该商品！")) {
				return;
			}
			var that = $(this);
			var htm = $(this).html();
			
			var delObj = $(this);
			jQuery.ajax({
				type : 'Get',
				contentType : 'application/json',
				url : '../admin/goods/offShelf.do',
				data : {
					id : delObj.data("id")
				},
				dataType : 'json',
				success : function(data) {
					if (data.result) {
						window.top.jWei.Tips.success("操作成功");
						if(htm == '下架'){
							that.html('<span style="color:red;">上架</span>');
						}else{
							that.html('下架');
						}
						//grid.reload();
						//window.location.reload(true);
					} else {
						window.top.jWei.Tips.success(data.errorMsg);
					}
				},
				error : function(data) {
					
				}
			});
		});

	</script>
</body>
</html>